// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SimpleVariableFile.java

package presenter;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class SimpleVariableFile
{

    public SimpleVariableFile()
    {
        var = new ArrayList();
        val = new ArrayList();
    }

    public SimpleVariableFile(File f)
    {
        var = new ArrayList();
        val = new ArrayList();
        int index = 0;
        file = f;
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(f));
            int i = 0;
            do
            {
                if(i >= 65535)
                    break;
                String temp = br.readLine();
                if(temp == null)
                    break;
                Scanner sc = new Scanner(temp);
                sc.useDelimiter("\\s*=\\s*");
                if(sc.hasNext())
                    var.add(sc.next());
                if(sc.hasNext())
                {
                    val.add(sc.next());
                    index++;
                }
                i++;
            } while(true);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public int getNumberOfVariables()
    {
        return var.size();
    }

    public boolean exists(String input)
    {
        Exception e;
        return getVariableIndex(input) >= 0;
        e;
        return false;
    }

    public String getVariable(String input)
    {
        for(int i = 0; i < var.size(); i++)
            if(((String)var.get(i)).equals(input))
                return (String)val.get(i);

        return "-1";
    }

    public String getVariable(String input, int start, int end)
    {
        int current = (start + end) / 2;
        int compare = input.compareTo((String)var.get(current));
        if(compare == 0)
            return (String)val.get(current);
        if(current == start || current == end)
            return "-1";
        if(compare > 0)
            return getVariable(input, current, end);
        else
            return getVariable(input, start, current);
    }

    public int getVariableIndex(String input)
    {
        for(int i = 0; i < var.size(); i++)
            if(((String)var.get(i)).equals(input))
                return i;

        return -1;
    }

    public int getVariableIndex(String input, int start, int end)
    {
        int current = (start + end) / 2;
        int compare = input.compareTo((String)var.get(current));
        if(compare == 0)
            return current;
        if(start > end)
            return -1;
        if(compare > 0)
            return getVariableIndex(input, current, end);
        else
            return getVariableIndex(input, start, current);
    }

    public void update(String variable[], String value[])
    {
        for(int i = 0; i < variable.length; i++)
            update(variable[i], value[i]);

    }

    public void update(String variable, String value)
    {
        int pos = getVariableIndex(variable);
        if(pos < 0)
            add(variable, value);
        else
            val.set(pos, value);
    }

    public void add(String variable, String value)
    {
        var.add(variable);
        val.add(value);
    }

    public void write(File file)
    {
        try
        {
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            for(int i = 0; i < var.size(); i++)
            {
                bw.write((String)var.get(i));
                bw.write(" = ");
                bw.write((String)val.get(i));
                bw.newLine();
            }

            bw.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void write()
    {
        write(file);
    }

    public static void main(String args[])
    {
        SimpleVariableFile svf = new SimpleVariableFile(new File("FramePositions.txt"));
    }

    File file;
    ArrayList var;
    ArrayList val;
}
